// 66. Plus One
// Given a non-negative number represented as an array of digits, plus one to the number.

// The digits are stored such that the most significant digit is at the head of the list.

package main

import "fmt"

func plusOne(digits []int) []int {
	count := len(digits)
	if count == 0 {
		return digits
	}

	incr, num := 1, 0
	for i := count - 1; i >= 0; i-- {
		num = digits[i] + incr
		if num < 10 {
			digits[i] = num
			return digits
		}
		digits[i] = num - 10
	}

	res := make([]int, count+1)
	copy(res[1:], digits)
	res[0] = 1
	return res
}

func main() {
	d := []int{9, 9, 9}
	r := plusOne(d)
	fmt.Printf("%v=%v\n", d, r)
}
